<?php
namespace app\common\validate\tysj;

class ProductExamineLog extends Base {
    /**
     * 验证规则
     */
    protected $rule = [
        'id'       => 'require',
        'pid'      => 'require|isUser:tysj\\Product',
        'admin_id' => 'require',
        'before'   => 'require',
        'to'       => 'require'
    ];

    /**
     * 提示消息
     */
    protected $message = [
        'lock.require'     => '验证规则场景不存在',
        'id.require'       => 'log id不能为空',
        'pid.require'      => '产品ID不能为空',
        'pid.isUser'       => '产品不属于您',
        'admin_id.require' => '管理员ID不能为空',
        'before.require'   => '修改前审核状态不能为空',
        'to.require'       => '修改后审核状态不能为空'
    ];
    /**
     * 验证场景
     */
    protected $scene = [
        'save'        => [
            'id',
            'pid',
            'admin_id',
            'before',
            'to'
        ],
        'new'         => ['pid', 'to']
    ];

    //验证修改的信息是否是当前用户的
    protected function isUser($value, $rule, $data = []) {
        $productModel = loadModel('tysj\\Product');
        $storeModel = loadModel('tysj\\Store');
        $product = $productModel->where(['id' => $data['pid']])->find();
        if(empty($product)) {
            return false;
        }
        $store = $storeModel->where(['id' => $product['sid']])->find();
        return $store && isset($store['uid']) && $store['uid'] == $data['uid'];
    }
}
